package com.team.recruitment_management_system.controller;

import com.team.recruitment_management_system.mapper.SysUserMapper;
import com.team.recruitment_management_system.pojo.dto.chat.AddChatListDto;
import com.team.recruitment_management_system.pojo.dto.message.AddChangeMessageDto;
import com.team.recruitment_management_system.pojo.entity.SysUser;
import com.team.recruitment_management_system.pojo.vo.chat.ChatListVo;
import com.team.recruitment_management_system.security.OperationLogAnnotation;
import com.team.recruitment_management_system.service.ChatListService;
import com.team.recruitment_management_system.service.ChatMessageService;
import com.team.recruitment_management_system.service.impl.ChatWebSocketServer;
import com.team.recruitment_management_system.util.JwtHelper;
import com.team.recruitment_management_system.util.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/chat")
@Tag(name = "聊天模块")
public class ChatController {
    @Autowired
    private ChatListService chatListService;

    @Autowired
    private ChatMessageService chatMessageService;

    @Autowired
    private ChatWebSocketServer chatWebSocketServer;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private JwtHelper jwtHelper;

    @GetMapping("/chatList")
    @Operation(summary = "求职者获取聊天列表", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "聊天模块", operType = "查询", operDesc = "求职者获取聊天列表")
    public Result<List<ChatListVo>> getMyCharListBySeeker(@RequestHeader String Authorization) {
        return chatListService.getMyCharListBySeeker(Authorization);
    }

    @GetMapping("/hr/chatList")
    @Operation(summary = "hr获取聊天列表", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "聊天模块", operType = "查询", operDesc = "hr获取聊天列表")
    public Result<List<ChatListVo>> getMyCharListByHr(@RequestHeader String Authorization) {
        return chatListService.getMyCharListByHr(Authorization);
    }

    @PostMapping("/chatList")
    @Operation(summary = "添加聊天对话", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "聊天模块", operType = "插入", operDesc = "添加聊天对话")
    public Result<Void> addCharList(@RequestBody AddChatListDto addChatListDto) {
        return chatListService.addCharList(addChatListDto);
    }

    @PostMapping("/message")
    @Operation(summary = "添加聊天信息", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "聊天模块", operType = "插入", operDesc = "添加聊天信息")
    public Result<Void> addChatMessage(@RequestBody AddChangeMessageDto addChangeMessageDto,@RequestHeader String Authorization) {
        Integer userId = jwtHelper.getUserId(Authorization);
        SysUser sysUser = sysUserMapper.selectById(userId);
        addChangeMessageDto.setSenderAvatar(sysUser.getAvatar());
        addChangeMessageDto.setSenderUsername(sysUser.getUsername());
        addChangeMessageDto.setSenderName(sysUser.getName());
        addChangeMessageDto.setCreateTime(new Date());
        chatWebSocketServer.sendMessage(addChangeMessageDto);
        return chatMessageService.addChatMessage(addChangeMessageDto);
    }

    @DeleteMapping("/chatList/{id}")
    @Operation(summary = "根据id删除聊天对话", security = {@SecurityRequirement(name = "Authorization")})
    @OperationLogAnnotation(operModul = "聊天模块", operType = "删除", operDesc = "根据id删除聊天对话")
    public Result<Void> deleteCharListById(@PathVariable Integer id) {
        return chatListService.deleteCharListById(id);
    }

}
